// 存放个人中心接口（获取用户信息、更新用户信息、重置密码、更换头像）

const express = require('express');

// 创建路由对象（实际上得到一个函数）
const router = express.Router();
// console.log(typeof router); // function

const db = require('../db');

// ------------------------------------------ 获取用户信息 ------------------------------------
router.get('/userinfo', (req, res) => {
  // console.log(req.user); // { id: 5, iat: 1620543046, exp: 1620550246 }
  db('select id, username, nickname, email, user_pic from user where id=' + req.user.id, (err, result) => {
    if (err) throw err;
    // console.log(result);
    res.send({ status: 0, message: '获取用户信息成功', data: result[0] })
  })
})

// ------------------------------------------ 更新头像 ------------------------------------
router.post('/avatar', (req, res) => {
  // console.log(req.user.id);  // 5
  // console.log(req.body.avatar); // adsfasdfasdf
  db(`update user set user_pic='${req.body.avatar}' where id=${req.user.id}`, (err, result) => {
    if (err) throw err;
    res.send({ status: 0, message: '更换成功' });
  })
})


module.exports = router;